var AjaxModal = function () {
	
	var formvalidata = function (modalId, module, method, rules) {

            var form = $('#' + modalId + " form");
            var error = $('.alert-error', form);
			
            form.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: rules,
				
                invalidHandler: function (event, validator) { //display error alert on form submit 
                    error.show();
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.help-inline').removeClass('ok'); // display OK icon
                    $(element)
                        .closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change dony by hightlight
                    $(element)
                        .closest('.control-group').removeClass('error'); // set error class to the control group
                },

                success: function (label) {
                    label
                        .addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
                    .closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
                },

                submitHandler: function (form) {
                    error.hide();
                    
                    conductAction(module, method, modalId);
                }
    	});
  	};
    
    var conductAction = function(module, method, modalId){
 			this.actionModule = module;
			this.action = $('#' + modalId + " #actionType").val();
			//this.method = method;
			this.data = $('#' + modalId + " form").serialize() + "&method=" + method;
			
			var modal = $('#' + modalId);
			
			this.url = hjjp_rootUrl + "/" + this.actionModule + "/" + this.action;
			$.post(this.url, this.data, function(resp){
				if(resp.status === 200){
					if(modal){
						modal.modal("hide");
					}
					window.setTimeout(window.location.reload(), 1000);
				}else{
					if(modal){
						modal.modal("hide");
					}
					$.scojs_message(resp.errorMsg, $.scojs_message.TYPE_ERROR);
				}
			}, "json");
 	};

    var initModal = function (modalId, module, method, rules) {
		
       	var $modal = $('#' + modalId);
 		
 		var clearInputVal = function(){
 			var $form = $('#' + modalId + " form");
 			$form.find("input[type=text]").each(function(){
 				$(this).val("");
 			});
 			$form.find("select").each(function(){
 				$(this).val("-1");
 			});
 			$form.find("input[type=radio]").each(function(){
 				if($(this).attr("data-default") == "true"){
 					$(this).parent().addClass("checked");
 				}else{
 					$(this).parent().removeClass("checked");
 				}
 			});
 			
 			$form.find(".alert-error").hide();
 		};
 		
		$('#create-act').on('click', function(){
			// create the backdrop and wait for next modal to be triggered
			$('body').modalmanager('loading');
			
			clearInputVal();
			
			$('#' + modalId + " #actionType").val("create");//标志actiontype为create
			
			setTimeout(function(){
			    $modal.modal();
			}, 500);
		});
   };

    return {
        //main function to initiate the module
        init: function (modalId, module, method, rules) {
            initModal(modalId, module, method, rules);
            
            formvalidata(modalId, module, method, rules);//初始化表单验证
        }

    };

}();